package com.mall.equipment.entity;

import com.mall.equipment.util.CommonConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author Simple
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class BaseResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    //返回标记：成功标记=0，失败标记=1
    private int code;
    @Getter
    @Setter
    //@ApiModelProperty(value = "返回信息")
    private String msg;
    @Getter
    @Setter
    //@ApiModelProperty(value = "数据")
    private T data;

    public static <T> BaseResponse<T> msg(String msg) {
        return restResult(null, CommonConstants.SUCCESS, msg);
    }

    public static <T> BaseResponse<T> ok() {
        return restResult(null, CommonConstants.SUCCESS, null);
    }

    public static <T> BaseResponse<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, null);
    }

    public static <T> BaseResponse<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> BaseResponse<T> ok(T data, int code, String msg) {
        return restResult(data, code, msg);
    }

    public static <T> BaseResponse<T> failed() {
        return restResult(null, CommonConstants.FAIL, null);
    }

    public static <T> BaseResponse<T> failed(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }

    public static <T> BaseResponse<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, null);
    }

    public static <T> BaseResponse<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }

    private static <T> BaseResponse<T> restResult(T data, int code, String msg) {
        BaseResponse<T> apiResult = new BaseResponse<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}